package chan.com.mvplib.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

import chan.com.mvplib.factory.PresenterMVPFactoryImpl;
import chan.com.mvplib.factory.PresenterMvpFactory;
import chan.com.mvplib.presenter.BaseMvpPresenter;
import chan.com.mvplib.proxy.BaseMvpProxy;
import chan.com.mvplib.proxy.PresenterProxyInterface;
import chan.com.mvplib.view.BaseMvpView;

/**
 * Created by chen on 2017/12/26.
 */
public class AbstractFragmentActivity<V extends BaseMvpView, P extends BaseMvpPresenter<V>> extends FragmentActivity implements PresenterProxyInterface<V, P> {

    BaseMvpProxy<V, P> mProxy = new BaseMvpProxy<>(PresenterMVPFactoryImpl.<V, P>createFactory(getClass()));
    private static final String PRESENTER_KEY = "presenter_save_key";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mProxy.onRestoreInstanceState(savedInstanceState.getBundle(PRESENTER_KEY));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mProxy.onResume((V) this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mProxy.onDestroy();
    }

    @Override
    public void setPresenterFactory(PresenterMvpFactory<V, P> presenterFactory) {
        mProxy.setPresenterFactory(presenterFactory);
    }

    @Override
    public PresenterMvpFactory<V, P> getPresenterFactory() {
        return mProxy.getPresenterFactory();
    }

    @Override
    public P getMvpPresenter() {
        return mProxy.getMvpPresenter();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle(PRESENTER_KEY, mProxy.onSaveInstanceState());
    }
}
